<?php
/**
 * BowlFramework Files
 * 
 * 这家伙太懒了，连注释都不写，鄙视他
 * 
 * @author armor
 * @date 13-7-10 下午4:52
 * 
 */
class Controller_Default extends Extension_Controller_FenderEngineBase{


    protected $debug=false;
    /**
     * 列表中每页的个数，分页时使用
     * @var int
     */
    protected $pLimit = 10;

    /**
     * 分页起始位置
     * @var int
     */
    protected $pStart = 0;

    /**
     * 当前页
     * 分页啊
     * @var int
     */
    protected $pNow = 1;
    protected function getPageInfo(){
        $this->pNow = $this->input->get("p",1);
        $this->pStart = ($this->pNow-1)*$this->pLimit;
    }
    public function _pager($totalRecord,$nowPage,$urlConfig=null,$perPage=null,$length=5){
        $perPage = is_null($perPage)?$this->pLimit:$perPage;

        $controller = isset($urlConfig[0])?$urlConfig[0]:null;
        $actionName = isset($urlConfig[1])?$urlConfig[1]:null;

        $appParams['app_controller'] = $controller;
        $appParams['app_action']   = $actionName;
        $appParams['app_id'] = $this->app['app_id'];

        $urlConfig = array("app","Index",$appParams);
        $jumpurl = $this->appUrl($controller,$actionName);

        $pagerHelper = new Bowl_Helper_Pager(array(
            "totalItems"=>$totalRecord,
            "perPage"=>$perPage,
            "currentPage"=>$nowPage,
            "pagerBodyLength"=>$length,
            "urlConfig" =>$urlConfig
        ));
        $pager= $pagerHelper->getPager();
        if(!empty($pager)){
            $pager['total'] = $totalRecord;
            $pager['start']  = ($nowPage-1)*$perPage + 1;
            $pager['end']    = $nowPage*$perPage < $totalRecord?$nowPage*$perPage:$totalRecord;
            $pager['lastpagernum']=ceil($totalRecord/$perPage);

            $pager['perpage'] = $perPage;
            $pager['nowpager'] = $nowPage;
            $pager['jumpurl'] = $jumpurl;
            $pager['totalpage'] = ceil($totalRecord/$perPage);

            $this->view->assign("pager",$pager);
        }
    }
    public function actionShow(){

        $mac = $_GET['mac'];
        $this->getPageInfo();
        $info=$this->appModel('Content')->getByAppId($this->app['app_id'],array(0,100));
        //todo 对错号=》重上传引擎 list.html使用图片
        //http://img.68design.net/art/images/5oW1RumE6NYE0ex.png 对号
        //http://img.68design.net/art/images/wT2GMidx2EOJggt.png 错号
        $this->_pager($info['total'],$this->pNow,array("default","index"));
        $this->view->assign("mac",$mac);
        $this->view->assign('info',$info['list']);
        $this->view->assign('total',$info['total']);
        $this->view->assign("emptySubPage","list.html");
        //$this->showAppPage("comment.html");
        //die(json_encode(array("success"=>true,"ok","data"=>$info['list'])));
        echo $_GET['callback'].'(' .json_encode(array("success"=>true,"ok","data"=>$info['list'])).')';
    }
    //增加建议
    public function actionCreate(){
      //  $post = $this->input->getPostParameters();
        $origContent = $_GET['content'];
        $mac = $_GET['mac'];
        $content = preg_replace("/#/",'', $origContent);
        $content=str_replace("\n"," ",$content);
        $rid= md5(uniqid(rand(),true));
        $createtime = date('Y-m-d H:i:s',time());
        $app = $this->getApp();
        $current = array(
            'advice_id' => $rid,
            'advice_content' => $content,
            'createtime' => $createtime,
            'app_id' => $app['app_id'],
            'advice_mac' => $mac

        );
        $this->appModel("Content")->create($current);
        //刷新首页
        $app = $this->getApp();
        $info=$this->appModel('Content')->getByAppId($this->app['app_id'],array(0,100));
        $this->view->assign("app_id",$app['app_id']);
        $this->view->assign("mac",$mac);
        $this->view->assign("info",$info['list']);
        echo $_GET['callback'].'(' .json_encode(array("success"=>true,"ok","data"=>'ok')).')';
        //$this->showAppPage("comment.html");
    }
    public function actionIndex(){
        //        dump($this->appPreviewDir);
        $this->getPageInfo();
        $info=$this->appModel('Content')->getByAppId($this->app['app_id'],array($this->pStart,$this->pLimit));
        //todo 对错号=》重上传引擎 list.html使用图片
        //http://img.68design.net/art/images/5oW1RumE6NYE0ex.png 对号
        //http://img.68design.net/art/images/wT2GMidx2EOJggt.png 错号
        $this->_pager($info['total'],$this->pNow,array("default","index"));
        $this->view->assign('info',$info['list']);
        $this->view->assign('total',$info['total']);
        $this->view->assign("emptySubPage","list.html");
        $this->showAppPage("container.html");
    }

    //在数据库创建
    public function actionFileSync(){
        $data = $this->input->post("data",false);
        if(false === $data)
        {
            exit(0);
        }
        $data = explode("#",$data);
        for($key=0 ;$key < count($data);$key=$key+4)
        {
            $arr = array(
                'advice_id'=>$data[$key+0],
                'advice_content'=>$data[$key+1],
                'advice_mac'=>$data[$key+2],
                'createtime'=>$data[$key+3],
                'app_id'=>$this->app['app_id']
            );
            $this->appModel("Content")->create($arr);
        }
        echo "OK";

    }
    public function actionDelete()
    {
        $advice_id = $this->input->get("advice_id",false);
        $this->appModel("Content")->delete($advice_id);
        $this->actionIndex();

        $app = $this->getApp();
        $advices = $this->appModel("Content")->getById($app['app_id']);
        $str = '';
        foreach($advices as $key=>$item){
            $str .= $item['advice_id']."#".$item['advice_content']."#".$item['advice_mac']."#".$item['createtime']."\r\n";
        }
        $file_name = $this->appPreviewDir."./data/display";
        $file=fopen($file_name,"a");
        fwrite($file,$str);
        fclose($file);
        //需要重新写入文件
    }


}